<?php
class VES_VendorsWarehouse_Block_Confirmation_Order_View extends Mage_Adminhtml_Block_Template
{
	public function __construct()
    {
		$this->setTemplate('ves_vendorswarehouse/confirmation/order/view.phtml');
    }

	public function getOrder()
	{
		$orderId = $this->getRequest()->getParam('id');
		$_order  = Mage::getModel('sales/order')->load($orderId, 'entity_id');
		return $_order;
	}
	
	public function getAllItems()
	{
		$orderId  = $this->getRequest()->getParam('id');
		$_order   = Mage::getModel('sales/order')->load($orderId, 'entity_id');
		$allItems = $_order->getAllVisibleItems();
		return $allItems;
	}
	
	public function getAllSubItems()
	{
		$allItems = $this->getAllItems();
		$allItemsArray = array();
		foreach($allItems as $item){
			$_product = $item->getProduct();
			$shippingType = $_product->getData('common_shipping_type');
			$shippingTypeLabel = $_product->getResource()->getAttribute('common_shipping_type')->setStoreId(0)
										  ->getFrontend()->getValue($_product);
			$allItemsArray[$shippingType]['shippingTypeLabel'] = $shippingTypeLabel;
			$allItemsArray[$shippingType]['qty_ordered'] = $item->getQtyOrdered();
			$allItemsArray[$shippingType]['name'] = $item->getName();
			$allItemsArray[$shippingType]['sku'] = $item->getSku();
			// $allItemsArray[$shippingType]['image'] = $this->getItemImageUrl($item);
			$allItemsArray[$shippingType]['weight'] = $item->getWeight();
		}
		return $allItemsArray;
	}
	
	public function getItemImageUrl($item)
	{
		$w = '80';
		$h = '80';
		$imageUrl = $this->getSkinUrl('ves_vendors/images/catalog/product/placeholder/small_image.jpg');

		$product  = Mage::getModel('catalog/product')->setStoreId(0)->loadByAttribute('sku', $item->getSku());
		if(!$product) return $imageUrl;
		$configurable_ids  = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
		if(count($configurable_ids) > 0){
			$productConfig = Mage::getModel('catalog/product')->setStoreId(0)->load($configurable_ids[0]);
			$colorText = $product->getResource()->getAttribute('common_color')->setStoreId(0)->getFrontend()->getValue($product);
			$_img = $productConfig->getMediaGalleryImages()->getItemByColumnValue('label',$colorText);
			if(!empty($colorText) && !empty($_img)){
				$imageUrl = Mage::helper('catalog/image')->init($productConfig, 'thumbnail', $_img->getFile())->resize($w, $h);
			}else{
				if($productConfig->getImage() != 'no_selection'){
					$imageUrl = Mage::helper('catalog/image')->init($product, 'thumbnail')->resize($w, $h);
				}
			}
			// $productUrl = $productConfig->getProductUrl();
		}else{
			if($product->getImage() != 'no_selection')
			{
				$imageUrl   = Mage::helper('catalog/image')->init($product, 'thumbnail')->resize($w, $h);
			}
			// $productUrl = $product->getProductUrl();
		}
		
		return $imageUrl;
	}
}
